package com.yuejian_06.yuejian_06.model;

import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.bean.ClubBean;
import com.yuejian_06.yuejian_06.bean.ClubVideoBean;
import com.yuejian_06.yuejian_06.bean.CourseBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.fragment.Constants_l;
import com.yuejian_06.yuejian_06.service.ClubService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by __ZL__ on 2017/9/25.
 */

public class ClubModel {

    private Retrofit retrofit;
    public ClubModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.SERVERADDRESS)//增加返回值为Gson的支持（一实体类返回）
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //俱乐部列表
    public void clubList( final LoginListener loginListener){
        ClubService clubService = retrofit.create(ClubService.class);
        Call<List<ClubBean>> call=clubService.clubList();
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }
    //俱乐部详情
    public void clubInfo( String club_id,final LoginListener loginListener){
        ClubService clubService = retrofit.create(ClubService.class);
        Call<ClubBean> call=clubService.getclub(club_id);
        call.enqueue(new Callback<ClubBean>() {
            @Override
            public void onResponse(Call<ClubBean> call, Response<ClubBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<ClubBean> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //俱乐部课程列表
    public void clubCourseCL( String club_id,final LoginListener loginListener){
        ClubService clubService = retrofit.create(ClubService.class);
        Call<List<CourseBean>> call=clubService.getclubCL(club_id);
        call.enqueue(new Callback<List<CourseBean>>() {
            @Override
            public void onResponse(Call<List<CourseBean>> call, Response<List<CourseBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<CourseBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //俱乐部视频列表
    public void clubCourseV( String club_id,final LoginListener loginListener){
        ClubService clubService = retrofit.create(ClubService.class);
        Call<List<ClubVideoBean>> call=clubService.getclubV(club_id);
        call.enqueue(new Callback<List<ClubVideoBean>>() {
            @Override
            public void onResponse(Call<List<ClubVideoBean>> call, Response<List<ClubVideoBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<ClubVideoBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //俱乐部列表---俱乐部搜索
    public void getclubSearch( String name,final LoginListener loginListener){
        ClubService clubService = retrofit.create(ClubService.class);
        Call<List<ClubBean>> call=clubService.getclubSearch(name);
        call.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }
    //俱乐部收藏／取消收藏
    public void CollectClub( String user_id,String club_id,final LoginListener loginListener){
        ClubService clubService = retrofit.create(ClubService.class);
        Call<SuccessBean> call=clubService.CollectClub(user_id,club_id);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }

    //判断是否收藏
    public void isCollect( String user_id,String club_id,final LoginListener loginListener){
        ClubService clubService = retrofit.create(ClubService.class);
        Call<SuccessBean> call=clubService.isCollect(user_id,club_id);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("获取数据为空或无数据！");
            }
            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {

                loginListener.onFail(t.toString());
            }
        });
    }
}
